using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Common;
using Entities;
using Dao;
namespace Dao
{
    /// <summary>
    /// Provides a way of query with RoleFunctionRelationData from SQL Server Database
    /// </summary>
    public class RoleFunctionRelationData
    {
        #region Variables private
        private IList<RoleFunctionRelation> _listRoleFunctionRelation = null;
        private RoleFunctionRelation _roleFunctionRelation = null; 
        #endregion

        #region Methods public
        /// <summary>
        /// Display information of RoleFunctionRelation
        /// </summary>
        /// <returns>List of RoleFunctionRelation</returns>
        public IList<RoleFunctionRelation> GetAll()
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            _listRoleFunctionRelation = new List<RoleFunctionRelation>();
            try
            {
                SqlDataReader reader = SQLDBHelper.ExecuteReader(conn, CommandType.StoredProcedure, "sp_RoleFunctionRelation_GetAll", null);
                while (reader.Read())
                {
                    _roleFunctionRelation = new RoleFunctionRelation();
                    if (!reader.IsDBNull(0))
                        _roleFunctionRelation.RoleID = reader.GetByte(0);
                    if (!reader.IsDBNull(0))
                        _roleFunctionRelation.FunctionID = reader.GetByte(1);
                    _listRoleFunctionRelation.Add(_roleFunctionRelation);
                }
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return _listRoleFunctionRelation;
        }
        /// <summary>
        /// Insert new RoleFunctionRelation
        /// </summary>
        /// <param name="roleFunctionRelation">Colection properties of RoleFunctionRelation</param>
        /// <returns>True is successful , False is unsuccessful</returns>
        public bool Insert(RoleFunctionRelation roleFunctionRelation)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int result = 0;
            try
            {
                SqlParameter[] parms = new SqlParameter[] { 
                                new SqlParameter("@RoleID", SqlDbType.TinyInt),
                                new SqlParameter("@FunctionID", SqlDbType.TinyInt)
                            };
                parms[0].Value = roleFunctionRelation.RoleID;
                parms[1].Value = roleFunctionRelation.FunctionID;

                result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_RoleFunctionRelation_Insert", parms);
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return result > 0 ? true : false;
        }
        /// <summary>
        /// Update for RoleFunctionRelation
        /// </summary>
        /// <param name="roleFunctionRelation">Colection properties want to update</param>
        /// <returns>True is successful , False is unsuccessful</returns>
        public bool Update(RoleFunctionRelation roleFunctionRelation)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int result = 0;
            try
            {
                SqlParameter[] parms = new SqlParameter[] { 
                            new SqlParameter("@RoleID", SqlDbType.TinyInt),
                            new SqlParameter("@FunctionID", SqlDbType.TinyInt)};

                parms[0].Value = roleFunctionRelation.RoleID;
                parms[1].Value = roleFunctionRelation.FunctionID;

                result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_RoleFunctionRelation_Update", parms);

            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return result > 0 ? true : false;
        }
        /// <summary>
        /// Delete a RoleFunctionRelation
        /// </summary>
        /// <param name="roleID">Id of RoleFunctionRelation want to delete</param>
        /// <returns>True is successful , False is unsuccessful</returns>
        public bool Delete(byte roleID)
        {
            SqlConnection conn = new SqlConnection(SQLDBHelper.CONN_STRING);
            int result = 0;
            try
            {
                SqlParameter[] parm = new SqlParameter[] { 
                        new SqlParameter("@RoleID", SqlDbType.TinyInt)};
                parm[0].Value = roleID;
                result = SQLDBHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "sp_RoleFunctionRelation_DeleteByRoleID", parm);
            }
            catch (Exception ex)
            {
                ErrorManagement.TrackLog(ex.Message);
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            return result > 0 ? true : false;
        } 
        #endregion
    }
}
